<template>
  <div class='article-container'>
    <el-card class="box-card">
      <!-- 标题 -->
      <div slot="header" class="clearfix">
        <Breadcrumb>评论管理</Breadcrumb>
      </div>

      <!-- 主体表格 -->
      <el-table
        v-loading="loading"
        :data="comments"
        style="width: 100%">
        <el-table-column
          prop="title"
          label="文章标题"
        >
        </el-table-column>

        <el-table-column
          prop="total_comment_count"
          label="总评论数"
        >
        </el-table-column>

        <el-table-column
          prop="fans_comment_count"
          label="粉丝评论数"
        >
        </el-table-column>

        <!-- 由于后端回传的数据是数值，而我们要显示出来是对应字符串，
        所以，这里不能直接使用status -->
        <el-table-column
          label="评论状态"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.comment_status">正常</el-tag>
            <el-tag v-else type="danger">关闭</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              :type="scope.row.comment_status?'danger':'primary'"
              circle
              @click="hSwitchComment(scope.index,scope.row)">
              {{scope.row.comment_status?'关闭':'打开'}}评论
              
              </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件
        它只是根据total和page-size来显示页码，而与具体的表格数据无关

        1. total是总条数数；
        默认情况，它会以每页10条进行计算总页数
        2. current-change。当点击页码时，它会触发,会传入当前的页码。
      -->
      <el-pagination
        :disabled="loading"
        style="margin-top:10px;"
        background
        layout="prev, pager, next"
        @current-change="hPageChange"
        :page-size="10"
        :total="total_count">
      </el-pagination>

    </el-card>
  </div>
</template>

<script>
import axios from 'axios'
import Breadcrumb from '@/components/breadCrumb.vue'
import JSONbig from 'json-bigint'

export default {
  name: 'CommentIndex',
  props: { },
  components: {
    Breadcrumb
  },
  data () {
    return {
      loading: false, // 是否加载
      total_count: 0,
      comments: [] ,// 评论数据,
      page:1,
      per_page:10,
      curPage:1,
      id:1,
      allow_comment:true
    }
  },
  computed: { },
  created () {
      this.getComment()
   },
  methods: {
    async getComment(){
        try {
            const token = localStorage.getItem('tokenInfo')
            const result = await axios({
                method:'get',
                url:'http://ttapi.research.itcast.cn/mp/v1_0/articles',
                headers:{
                     Authorization:`Bearer ${token}`
                  },
                params:{
                   page:this.curPage,              
                   response_type: 'comment'
                }
            })
            console.log(result);
            this.comments = result.data.data.results
            this.total_count = result.data.data.total_count
            // this.id = JSONbig.parse(result.data.data.id).toString()
            
        } catch(err){
            console.log(err);
        }   
    },
    
    async hSwitchComment (index,row) {
      // 1.如果当前是打开的状态，则要改成关闭
      // this.id=JSONbig.parse(index.id).toString()
      // console.log(row);
      const token = localStorage.getItem('tokenInfo')
      try {
        const result = await axios({
        method:'put',
        url:'http://ttapi.research.itcast.cn/mp/v1_0/comments/status',
        params:{
          article_id:row.id.toString()
        },
        headers:{
          Authorization:`Bearer ${token}`
        },
        data:{
          allow_comment:!row.comment_status
        }
      })
        this.getComment()
      } catch(err){
        console.log(err);
      }

    },
    hPageChange (curPage) {
        console.log(curPage);
        this.curPage = curPage
        this.getComment()
    }
  }
}
</script>

<style scoped lang='less'></style>
